package com.loveyukari.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.loveyukari.dao.RegisterDao;
import com.loveyukari.model.AuthRegister;
import com.loveyukari.model.Register;

/**
*
*yangshaohua
*2017年1月23日下午8:19:31
**/

@Repository
public class JdbcRegisterDao extends BaseDao implements RegisterDao{

	private RowMapper<AuthRegister> authRegisterRowMapper = new RowMapper<AuthRegister>(){

		@Override
		public AuthRegister mapRow(ResultSet rs, int i) throws SQLException {
			AuthRegister authRegister = new AuthRegister();
			authRegister.setAuthId(rs.getInt("auth_id"));
			authRegister.setRegisterId(rs.getInt("register_id"));
			authRegister.setStatus(rs.getInt("status"));
			authRegister.setCreateTime(rs.getTimestamp("create_time"));
			return authRegister;
		}
	};
	
	
	public AuthRegister selectAuthRegister(int registerId, int authId) {
		String sql = "select * from auth_register where auth_id = ? and register_id = ?";
		List<AuthRegister> list = this.getJdbcTemplate().query(sql, authRegisterRowMapper,authId,registerId);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	public void updateAuthRegister(int registerId, int userId, int status) {
		String sql = "update auth_register set status = ? where auth_id = ? and register_id = ?";
		this.getJdbcTemplate().update(sql,status,userId,registerId);
	}

	@Override
	public int countAuthRegister(int userId, int status) {
		String sql = "select count(*) from auth_register where auth_id = ? and status = ?";
		Integer count = this.getJdbcTemplate().queryForObject(sql, Integer.class, userId, status);
		return count;
	}

	private class RegisterRowMapper implements RowMapper<Register>{

		public Register mapRow(ResultSet rs, int rowNum) throws SQLException {
			int id = rs.getInt("id");
			Timestamp startTime = rs.getTimestamp("start_time");
			Register register = new Register();
			register.setId(id);
			register.setStartTime(startTime);
			return register;
		}

		
	}

	public List<Register> selectListByTime(String startTime, String endTime) {
		String sql = "select * from register where start_time between ? and ?";
		List<Register> list = this.getJdbcTemplate().query(sql, new RegisterRowMapper(),startTime,endTime);
		return list;
	}

	public List<Register> selectListByTimeAndUser(String startTime, String endTime,int userId) {
		String sql = "select * from register where start_time between ? and ?";
		List<Register> list = this.getJdbcTemplate().query(sql, new RegisterRowMapper(),startTime,endTime);
		return list;
	}
	
	public void startRegister() {
		String sql = "insert into register(start_time) values(now())";
		this.getJdbcTemplate().update(sql);
	}

	public Register selectByLast() {
		String sql = "select * from register where status = 0 order by id desc limit 1";
		List<Register> list = this.getJdbcTemplate().query(sql, new RegisterRowMapper());
		if (list.isEmpty()){
			return null;
		}
		return list.get(0);
	}

	@Override
	public Register selectById(int id) {
		String sql = "select * from register where id = ?";
		List<Register> list = this.getJdbcTemplate().query(sql, new RegisterRowMapper(),id);
		if (list.isEmpty()){
			return null;
		}
		return list.get(0);
	}

	public void stopRegister() {
		String sql = "update register set status = 1";
		this.getJdbcTemplate().update(sql);
		
	}

	public void insertAuthRegister(int authId, int registerId, int status) {
		String sql = "insert into auth_register (auth_id,register_id,status,create_time) values(?,?,?,now())";
		this.getJdbcTemplate().update(sql,authId,registerId,status);
		
	}

	public List<Register> selectRegisterList(int start, int size) {
		String sql = "select * from register order by id desc limit ?,?";
		List<Register> list = this.getJdbcTemplate().query(sql, new RegisterRowMapper(),start,size);
		return list;
	}

	public List<Register> selectAll(){
		String sql = "select * from register";
		List<Register> list = this.getJdbcTemplate().query(sql, new RegisterRowMapper());
		return list;
	}

	public int countRegister() {
		String sql = "select count(*) from register";
		Integer count = this.getJdbcTemplate().queryForObject(sql, Integer.class);
		return count;
	}

	public List<AuthRegister> selectAuthRegisterList(int authId){
		String sql = "select * from auth_register where auth_id = ?";
		return this.getJdbcTemplate().query(sql,authRegisterRowMapper,authId);
	}


		
	
}
